using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using System.Web;
using System.Drawing;


namespace KbsUmlEditor
{
    class XMLparser
    {
        

        public static UMLDiagram ParseObjects(TreeView tr,UMLDiagram di,XmlReaderSettings m_Settings,String m_FileName)
        {

            XmlReader reader = XmlReader.Create(m_FileName, m_Settings);
            di = new UMLDiagram("geparsed");
            while (reader.Read())
            {
                Console.WriteLine(reader);
                switch (reader.NodeType)
                {
                    case XmlNodeType.Element:
                        if (!reader.IsEmptyElement)
                        {
                            switch (reader.Name)//haal de umlobjecten er uit er verwerk
                            {

                                case "Klasse":
                                    XmlReader innerklasse = reader.ReadSubtree();
                                    InsertKlasse(innerklasse, di);
                                    break;
                                case "Lijn":
                                    XmlReader innerlijn = reader.ReadSubtree();
                                    InsertLijn(innerlijn, di);
                                    break;
                                case "Notitie":
                                    XmlReader innernotitie = reader.ReadSubtree();
                                    InsertNotitie(innernotitie, di);
                                    break;
                            }

                        } break;
                    default:

                        break;

                }

            }

            reader.Close();
            BuildTree(tr, di);
            return di;
        }

        public static void InsertKlasse(XmlReader reader,UMLDiagram di)
        {
            //          <Klasse>
            //  <Name>klasse</Name>
            //  <BeginPositie>
            //    <X>2255</X>
            //    <Y>4</Y>
            //  </BeginPositie>
            //  <EindPositie>
            //    <Width>400</Width>
            //    <Heigth>30</Heigth>
            //  </EindPositie>
            //</Klasse>
            string naam, b_x, b_y, e_x, e_y;
            reader.ReadToFollowing("Name");
            reader.Read();
            naam = reader.Value;
            reader.ReadToFollowing("X");
            reader.Read();
            b_x = reader.Value;
            reader.ReadToFollowing("Y");
            reader.Read();
            b_y = reader.Value;
            reader.ReadToFollowing("X");
            reader.Read();
            e_x = reader.Value;
            reader.ReadToFollowing("Y");
            reader.Read();
            e_y = reader.Value;
            Point begin = new Point(int.Parse(b_x), int.Parse(b_y));
            Point eind = new Point(int.Parse(e_x), int.Parse(e_y));
            UMLKlasse klasse = new UMLKlasse(begin, eind, naam);
            di.VoegToeUMLObject(klasse);
        }

        public static void InsertLijn(XmlReader reader, UMLDiagram di)
        {
            
            string  b_x, b_y, e_x, e_y, begintype, eindtype;
            
            reader.ReadToFollowing("X");
            reader.Read();
            b_x = reader.Value;
            reader.ReadToFollowing("Y");
            reader.Read();
            b_y = reader.Value;
            reader.ReadToFollowing("X");
            reader.Read();
            e_x = reader.Value;
            reader.ReadToFollowing("Y");
            reader.Read();
            e_y = reader.Value;
            reader.ReadToFollowing("BeginPuntType");
            reader.Read();
            begintype = reader.Value;
            reader.ReadToFollowing("EindPuntType");
            reader.Read();
            eindtype = reader.Value;
            Point begin = new Point(int.Parse(b_x), int.Parse(b_y));
            Point eind = new Point(int.Parse(e_x), int.Parse(e_y));
            UMLLijn lijn = new UMLLijn(begin, eind, UMLPunt.getType(begintype), UMLPunt.getType(eindtype));
            di.VoegToeUMLObject(lijn);
        }

        public static void InsertNotitie(XmlReader reader, UMLDiagram di)
        {

            string  b_x, b_y, e_x, e_y, notitie;

            reader.ReadToFollowing("X");
            reader.Read();
            b_x = reader.Value;
            reader.ReadToFollowing("Y");
            reader.Read();
            b_y = reader.Value;
            reader.ReadToFollowing("X");
            reader.Read();
            e_x = reader.Value;
            reader.ReadToFollowing("Y");
            reader.Read();
            e_y = reader.Value;
            reader.ReadToFollowing("Notitie");
            reader.Read();
            notitie = reader.Value;
            
           
            Point begin = new Point(int.Parse(b_x), int.Parse(b_y));
            Point eind = new Point(int.Parse(e_x), int.Parse(e_y));
            UMLNotitie umlnotitie = new UMLNotitie(begin, eind);
            umlnotitie.Notitie = notitie;
            di.VoegToeUMLObject(umlnotitie);
        }

        public static void BuildTree(TreeView tr, UMLDiagram di)
        {
            tr.Nodes.Clear();
            tr.BeginUpdate();
            tr.Nodes.Add("uml diagram");//umldiagram naam
            tr.Nodes[0].Nodes.Add("diagram naam");
            int i = 1;
            if (di == null)
            {
                return;
            }
            foreach (UMLObject umlobject in di.UMLObjectsList)
            {
                //klasse
                if (umlobject.GetType() == typeof(UMLKlasse))
                {
                    UMLKlasse klasse = (UMLKlasse)umlobject;

                    tr.Nodes[0].Nodes.Add("Klasse");//+
                    tr.Nodes[0].Expand();
                    tr.Nodes[0].Nodes[i].Nodes.Add("Naam");//+
                    tr.Nodes[0].Nodes[i].Nodes[0].Nodes.Add(klasse.Naam);

                    tr.Nodes[0].Nodes[i].Nodes.Add("BeginPunt");//+
                    tr.Nodes[0].Nodes[i].Nodes[1].Nodes.Add(klasse.BeginPositie.ToString());

                    tr.Nodes[0].Nodes[i].Nodes.Add("EindPunt");//+
                    tr.Nodes[0].Nodes[i].Nodes[2].Nodes.Add(klasse.EindPositie.ToString());

                }
                //lijn
                if (umlobject.GetType() == typeof(UMLLijn))
                {
                    UMLLijn lijn = (UMLLijn)umlobject;

                    tr.Nodes[0].Nodes.Add("Lijn");//+
                    //tr.Nodes[0].Expand();
                    //tr.Nodes[0].Nodes[i].Nodes.Add("Naam");//+
                    //tr.Nodes[0].Nodes[i].Nodes[0].Nodes.Add(lijn.Naam);

                    tr.Nodes[0].Nodes[i].Nodes.Add("BeginPunt");//+
                    tr.Nodes[0].Nodes[i].Nodes[0].Nodes.Add(lijn.BeginPositie.ToString());

                    tr.Nodes[0].Nodes[i].Nodes.Add("EindPunt");//+
                    tr.Nodes[0].Nodes[i].Nodes[1].Nodes.Add(lijn.EindPositie.ToString());

                    tr.Nodes[0].Nodes[i].Nodes.Add("BeginPuntType");//+
                    tr.Nodes[0].Nodes[i].Nodes[2].Nodes.Add(lijn.BeginPunt.ToString());

                    tr.Nodes[0].Nodes[i].Nodes.Add("EindPuntType");//+
                    tr.Nodes[0].Nodes[i].Nodes[3].Nodes.Add(lijn.EindPunt.ToString());
                }

                //notitie
                if (umlobject.GetType() == typeof(UMLNotitie))
                {
                    UMLNotitie notitie = (UMLNotitie)umlobject;

                    tr.Nodes[0].Nodes.Add("Notitie");//+
                    //tr.Nodes[0].Expand();

                    tr.Nodes[0].Nodes[i].Nodes.Add("BeginPunt");//+
                    tr.Nodes[0].Nodes[i].Nodes[0].Nodes.Add(notitie.BeginPositie.ToString());

                    tr.Nodes[0].Nodes[i].Nodes.Add("EindPunt");//+
                    tr.Nodes[0].Nodes[i].Nodes[1].Nodes.Add(notitie.EindPositie.ToString());

                    tr.Nodes[0].Nodes[i].Nodes.Add("Notitie");//+
                    tr.Nodes[0].Nodes[i].Nodes[2].Nodes.Add(notitie.Notitie);
                }
                i++;
            }
            tr.EndUpdate();
            //tr.ExpandAll();
            tr.Refresh();
        }

        public static void SaveDiagramToXml(string filename, UMLDiagram di)
        {
            try
            {


                string m_Document = filename;

                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Indent = true;
                settings.NewLineOnAttributes = true;
                XmlWriter writer = XmlWriter.Create(m_Document, settings);
                writer.WriteStartDocument();
                // Write the root element.
                writer.WriteStartElement("UML_objects");
                //writer.WriteStartElement("Klasse");
                writer.WriteElementString("Name", di.Naam);
                //writer.WriteEndElement();

                foreach (UMLObject umlobject in di.UMLObjectsList)
                {
                    //klasse
                    if (umlobject.GetType() == typeof(UMLKlasse))
                    {
                        UMLKlasse klasse = (UMLKlasse)umlobject;
                        writer.WriteStartElement("Klasse");
                            writer.WriteElementString("Name", klasse.Naam);
                            writer.WriteStartElement("BeginPositie");
                                writer.WriteElementString("X", klasse.BeginPositie.X.ToString());
                                writer.WriteElementString("Y", klasse.BeginPositie.Y.ToString());
                                writer.WriteFullEndElement();
                            writer.WriteStartElement("EindPositie");
                                writer.WriteElementString("X", klasse.EindPositie.X.ToString());
                                writer.WriteElementString("Y", klasse.EindPositie.Y.ToString());
                                writer.WriteFullEndElement();
                        writer.WriteEndElement();


                    }
                    //Lijn
                    if (umlobject.GetType() == typeof(UMLLijn))
                    {
                        UMLLijn lijn = (UMLLijn)umlobject;
                        writer.WriteStartElement("Lijn");
                        //writer.WriteElementString("Name", lijn.Naam);
                            writer.WriteStartElement("BeginPositie");
                                writer.WriteElementString("X", lijn.BeginPositie.X.ToString());
                                writer.WriteElementString("Y", lijn.BeginPositie.Y.ToString());
                                writer.WriteFullEndElement();
                            writer.WriteStartElement("EindPositie");
                                writer.WriteElementString("X", lijn.EindPositie.X.ToString());
                                writer.WriteElementString("Y", lijn.EindPositie.Y.ToString());
                                writer.WriteFullEndElement();

                            writer.WriteElementString("BeginPuntType", lijn.BeginPunt.ToString());
                            writer.WriteElementString("EindPuntType", lijn.EindPunt.ToString());
                        writer.WriteFullEndElement();
                        //writer.WriteEndElement();


                    }
                    //Notitie
                    if (umlobject.GetType() == typeof(UMLNotitie))
                    {
                        UMLNotitie notitie = (UMLNotitie)umlobject;
                        writer.WriteStartElement("Notitie");
                        //writer.WriteElementString("Name", lijn.Naam);
                            writer.WriteStartElement("BeginPositie");
                                writer.WriteElementString("X", notitie.BeginPositie.X.ToString());
                                writer.WriteElementString("Y", notitie.BeginPositie.Y.ToString());
                                writer.WriteFullEndElement();
                            writer.WriteStartElement("EindPositie");
                                writer.WriteElementString("X", notitie.EindPositie.X.ToString());
                                writer.WriteElementString("Y", notitie.EindPositie.Y.ToString());
                                writer.WriteFullEndElement();

                            writer.WriteElementString("Notitie", notitie.Notitie);
                       
                        writer.WriteFullEndElement();
                        //writer.WriteEndElement();


                    }
                }
                // Write an element with attributes.
                writer.WriteEndElement();
                writer.WriteEndDocument();

                // Write the XML to file and close the writer
                writer.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }


        }
    }
}
